package bhbt.controller.member.wealth;


import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.log.LogEleService;
import bhbt.service.log.LogMoneyPayService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;






@Controller
 
public class ZhuanController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogMoneyPayService logMoneyPayService;
	@Resource
	private LogEleService logEleService;

	@Resource
	private ParamWageService paramWageService;

	@RequestMapping("member/wealth/eleZhuanMoneyShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);
		
		ParamWage paramWage1000=paramWageService.getByKey(1000);
		request.setAttribute("paramWage1000",paramWage1000.getValue());
		
		ParamWage paramWage1200=paramWageService.getByKey(1200);
		request.setAttribute("paramWage1200",paramWage1200.getValue());
		ParamWage paramWage1201=paramWageService.getByKey(1201);
		request.setAttribute("paramWage1201",paramWage1201.getValue());
		ParamWage paramWage1202=paramWageService.getByKey(1202);
		request.setAttribute("paramWage1202",paramWage1202.getValue());
		ParamWage paramWage1203=paramWageService.getByKey(1203);
		request.setAttribute("paramWage1203",paramWage1203.getValue());
		ParamWage paramWage1204=paramWageService.getByKey(1204);
		request.setAttribute("paramWage1204",paramWage1204.getValue());
		ParamWage paramWage1205=paramWageService.getByKey(1205);
		request.setAttribute("paramWage1205",paramWage1205.getValue());
		 
		
		return "member/wealth/eleZhuanMoney";
	}

	 
	
	 
	
	@RequestMapping("member/wealth/eleZhuanMoney.shtml")
	public String pay(HttpServletRequest request, ModelMap model) throws Exception{
		
		Boolean result=true;
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		 
		Map formMap = getParameterMap(request);
		String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
   	 
    	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"安全密码不正确。");
    		result=false;
    	}else{
    		
    		 
    		//转账金额验证
    		Double num = MapUtil.getDoubleValue(formMap, "num");
    		 
    		if(num<0){
    			setMessage(model,"请输入正确的转换数。");
        		result=false;
    		}else{
    			if(member.getEle()<num){
    				setMessage(model,"原石不足");
            		result=false;
    			}else{
    				 
    				logEleService.saveZhuan(num);
    				return "redirect:logEleList.shtml";
    				
    			}
    			
    		}
			 
			
			 
		 
    		 
    	}
		
    	if(!result){
    		
    		request.setAttribute("member",member);
    		ParamWage paramWage1000=paramWageService.getByKey(1000);
    		request.setAttribute("paramWage1000",paramWage1000.getValue());
    		
    		ParamWage paramWage1200=paramWageService.getByKey(1200);
    		request.setAttribute("paramWage1200",paramWage1200.getValue());
    		ParamWage paramWage1201=paramWageService.getByKey(1201);
    		request.setAttribute("paramWage1201",paramWage1201.getValue());
    		ParamWage paramWage1202=paramWageService.getByKey(1202);
    		request.setAttribute("paramWage1202",paramWage1202.getValue());
    		ParamWage paramWage1203=paramWageService.getByKey(1203);
    		request.setAttribute("paramWage1203",paramWage1203.getValue());
    		ParamWage paramWage1204=paramWageService.getByKey(1204);
    		request.setAttribute("paramWage1204",paramWage1204.getValue());
    		ParamWage paramWage1205=paramWageService.getByKey(1205);
    		request.setAttribute("paramWage1205",paramWage1205.getValue());
    		 
    		return "member/wealth/eleZhuanMoney";
    	}
		
		return "redirect:logEleList.shtml";
	}
}
